package com.lgl.controller;

import com.lgl.service.ICodeService;
import com.lgl.service.IEmailService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/code")
public class CodeController {

    @Autowired
    private ICodeService codeService;

    @Reference
    private IEmailService emailService;

    @GetMapping("/create/{email}")
    public String generateCode(@PathVariable String email) {
        String code = codeService.generateCode(email);
        boolean result = emailService.sendEmail(email, code);
        if (result) {
            return code;
        }
        return "";
    }

    @GetMapping("/validate/{email}/{code}")
    public int validateCode(@PathVariable String email, @PathVariable String code) {
        return codeService.validateCode(email, code);
    }
}
